﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Twilight.Controls;

namespace Twilight.Tests
{
    [TestClass]
    public class TweetBlockFixture : SilverlightTest
    {
        [TestMethod]
        public void TwitterUserLinks()
        {
            string testText = "@bryantlikes doing some Silverlight testing.";

            TweetBlock tb = new TweetBlock() { Text = testText };

            Assert.AreEqual<int>(5, tb.Children.Count); // should be five words
            Assert.AreEqual<Type>(typeof(HyperlinkButton), tb.Children[0].GetType()); // first link should be a hyperlink
            Assert.AreEqual<Uri>(new Uri("http://twitter.com/bryantlikes"), ((HyperlinkButton)tb.Children[0]).NavigateUri);
        }

        [TestMethod]
        public void TwitterUserLinkInBody()
        {
            string testText = "Saw @bryantlikes running tests.";

            TweetBlock tb = new TweetBlock() { Text = testText };

            Assert.AreEqual<int>(4, tb.Children.Count); // should be four words
            Assert.AreEqual<Type>(typeof(HyperlinkButton), tb.Children[1].GetType());
            Assert.AreEqual<Uri>(new Uri("http://twitter.com/bryantlikes"), ((HyperlinkButton)tb.Children[1]).NavigateUri);
        }

        [TestMethod]
        public void TwitterUserLinkAtEnd()
        {
            string testText = "He is running tests @bryantlikes";

            TweetBlock tb = new TweetBlock() { Text = testText };

            Assert.AreEqual<int>(5, tb.Children.Count); // should be five words
            Assert.AreEqual<Type>(typeof(HyperlinkButton), tb.Children[4].GetType());
            Assert.AreEqual<Uri>(new Uri("http://twitter.com/bryantlikes"), ((HyperlinkButton)tb.Children[4]).NavigateUri);
        }

        [TestMethod]
        public void TwitterUserLinkInRetweet()
        {
            string testText = "RT: @bryantlikes: running some tests";

            TweetBlock tb = new TweetBlock() { Text = testText };

            Assert.AreEqual<int>(6, tb.Children.Count); // should be six words (note: the ":" is a word since it isn't part of the link)
            Assert.AreEqual<Type>(typeof(HyperlinkButton), tb.Children[1].GetType());
            Assert.AreEqual<Uri>(new Uri("http://twitter.com/bryantlikes"), ((HyperlinkButton)tb.Children[1]).NavigateUri);
        }

        [TestMethod]
        public void TwitterNormalLinks()
        {
            string testText = "My blog is at http://blogs.sqlxml.org/bryantlikes";

            TweetBlock tb = new TweetBlock() { Text = testText };

            Assert.AreEqual<int>(5, tb.Children.Count); // should be five words
            Assert.AreEqual<Type>(typeof(HyperlinkButton), tb.Children[4].GetType()); // first link should be a hyperlink
            Assert.AreEqual<Uri>(new Uri("http://blogs.sqlxml.org/bryantlikes"), ((HyperlinkButton)tb.Children[4]).NavigateUri);
        }

        [TestMethod]
        public void TwitterNormalLinksAtBeginning()
        {
            string testText = "http://blogs.sqlxml.org/bryantlikes is my blog url";

            TweetBlock tb = new TweetBlock() { Text = testText };

            Assert.AreEqual<int>(5, tb.Children.Count); // should be five words
            Assert.AreEqual<Type>(typeof(HyperlinkButton), tb.Children[0].GetType()); // first link should be a hyperlink
            Assert.AreEqual<Uri>(new Uri("http://blogs.sqlxml.org/bryantlikes"), ((HyperlinkButton)tb.Children[0]).NavigateUri);
        }

        [TestMethod]
        public void TwitterNormalLinksInsideParens()
        {
            string testText = "My blog (http://blogs.sqlxml.org/bryantlikes) is updated";

            TweetBlock tb = new TweetBlock() { Text = testText };

            Assert.AreEqual<int>(7, tb.Children.Count); // should be seven words, the parens are each a seperate word
            Assert.AreEqual<Type>(typeof(HyperlinkButton), tb.Children[3].GetType()); 
            Assert.AreEqual<Uri>(new Uri("http://blogs.sqlxml.org/bryantlikes"), ((HyperlinkButton)tb.Children[3]).NavigateUri);
        }

        [TestMethod]
        public void TwitterNormalLinksWithAllSpecialChars()
        {
            string testText = "This is a link http://some-fake.com/bryantlikes?test=pass&code_coverage=decent";

            TweetBlock tb = new TweetBlock() { Text = testText };

            Assert.AreEqual<int>(5, tb.Children.Count); // should be seven words, the parens are each a seperate word
            Assert.AreEqual<Type>(typeof(HyperlinkButton), tb.Children[4].GetType());
            Assert.AreEqual<Uri>(new Uri("http://some-fake.com/bryantlikes?test=pass&code_coverage=decent"), ((HyperlinkButton)tb.Children[4]).NavigateUri);
        }

        [TestMethod]
        public void AddTweetBlockToPanel()
        {
            string testText = "This is a link http://some-fake.com/bryantlikes?test=pass&code_coverage=decent";

            TweetBlock tb = new TweetBlock() { Text = testText };

            TestPanel.Children.Add(tb);

            Assert.IsTrue(true);
        }
    }
}
